package com.aletto.general_store.ui.popup;

import android.content.Context;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;

import com.aletto.general_store.R;
import com.aletto.general_store.data.entities.WalletAccount;
import com.aletto.general_store.data.ERC20ServiceAbi;
import com.aletto.general_store.utils.LocalConfigStore;
import com.aletto.general_store.utils.ToastUtils;
import com.lxj.xpopup.core.CenterPopupView;
import com.sisyphus.spinach.network.utils.RxUtil;

import org.web3j.crypto.Credentials;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.utils.Convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.function.Consumer;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class ApprovePermissionPopup extends CenterPopupView {

    private Consumer<String> consumer;

    public ApprovePermissionPopup(@NonNull Context context) {
        super(context);
    }

    public ApprovePermissionPopup(@NonNull Context context, Consumer<String> consumer) {
        super(context);
        this.consumer = consumer;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_approve_permission;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        View progress = findViewById(R.id.progress);

        Button button_cancel = findViewById(R.id.button_cancel);
        button_cancel.setOnClickListener(v -> dismiss());

        Button button_confirm = findViewById(R.id.button_confirm);
        button_confirm.setOnClickListener(v -> {
            progress.setVisibility(View.VISIBLE);

            WalletAccount wallet = LocalConfigStore.getInstance().getWallet();
            Observable.create((ObservableOnSubscribe<String>) emitter -> {

                        EthSendTransaction ethSendTransaction = ERC20ServiceAbi.approve(
//                                ERC20ServiceAbi.get(),
//                                BigInteger.valueOf(97).byteValue(),
                                Credentials.create(wallet.getPrivateKey()),
                                ERC20ServiceAbi.MARKET_CONTRACT_ADDRESS,
                                ERC20ServiceAbi.USDT_CONTRACT_ADDRESS,
                                Convert.toWei(new BigDecimal(Integer.MAX_VALUE), Convert.Unit.ETHER).toBigInteger()
                        );

                        emitter.onNext(ethSendTransaction.getTransactionHash());
                        emitter.onComplete();
                    })
                    .compose(RxUtil.transformer())
                    .subscribe(new Observer<>() {
                        @Override
                        public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                            consumer.accept(s);
                        }

                        @Override
                        public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                            dismiss();
                            ToastUtils.makeText(e.getLocalizedMessage());
                        }

                        @Override
                        public void onComplete() {
                            dismiss();
                            progress.setVisibility(View.GONE);
                        }

                    });
        });
    }

}
